﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EBEInventory_DAL;

public partial class Inventory_Loc_SubLoc_Mangement_Default : System.Web.UI.Page
{
    InventoryEntities ctx = null;
    bool done = false;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string opType = Server.UrlDecode(Request.QueryString["Operation"]);

            if (string.Compare(opType, "Register") == 0)
            {
                ViewState["Operation"] = btn_InvRegister.Text = opType;
                
            }

            else if (string.Compare(opType, "Edit") == 0)
            {
                ViewState["Operation"] = btn_InvRegister.Text = opType;
                
            }

            else if (string.Compare(opType, "Delete") == 0)
            {
                ViewState["Operation"] = btn_InvRegister.Text = opType;
                
            }

            Inventory inv = (Inventory)Session["SelectedInventory"];
            if (inv != null)
            {
                HiddenID.Value = inv.ID.ToString();
                txt_InvName.Text = inv.Name;
                txt_InvAddress.Text = inv.Address;
                txt_InvPhone.Text = inv.Phone;
                txt_InvMobile.Text = inv.Mobile;
                txt_InvNotes.Text = inv.Notes;
            }
        }

        else
        {
             ctx = InitConnection.Initialize();
        }
    }

    protected void btn_InvRegister_Click(object sender, EventArgs e)
    {
        
        Dictionary<string, string> inv = new Dictionary<string, string>();

        inv["ID"] = HiddenID.Value;
        inv["Name"] = txt_InvName.Text;
        inv["Address"] = txt_InvAddress.Text;
        inv["Phone"] = txt_InvPhone.Text;
        inv["Mobile"] = txt_InvMobile.Text;
        inv["Notes"] = txt_InvNotes.Text;

        if (string.Compare(ViewState["Operation"].ToString(), "Register") == 0)
        {
            try
            {
                InvOps.AddInventory(inv, ctx);
                done = true;
            }

            catch (Exception ex)
            {
                lbl_errMsg.Text = ex.InnerException.Message;
                done = false;
            }
        }

        else if (string.Compare(ViewState["Operation"].ToString(), "Edit") == 0)
        {
            try
            {
                InvOps.EditInventory(inv, ctx);
                done = true;
            }

            catch (Exception ex)
            {
                lbl_errMsg.Text = ex.InnerException.Message;
                done = false;
            }
        }

        else if (string.Compare(ViewState["Operation"].ToString(), "Delete") == 0)
        {
            try
            {
                InvOps.DeleteInventory(Convert.ToInt32(inv["ID"]), ctx);
                done = true;
            }

            catch (Exception ex)
            {
                lbl_errMsg.Text = ex.InnerException.Message;
                done = false;
            }
        }

        if(done)
        Response.Redirect("~/Inventory/Loc-SubLoc Mangement/ListLoc-Sub.aspx");
    }

}